﻿from lexer import lex # Cambiar el import al renombrar el archivo del lexer:

def getTokens(text):
	lex.lexer.input(text)
	token = lex.lexer.token()
	while token:
		yield token
		token = lex.lexer.token()

def lex_test(text, expectedValues):
	print 'Input: %r' % text
	tokenValues = [t.value for t in getTokens(text)]
	print '\ttokens: %s. ' % tokenValues,
	if expectedValues != tokenValues:
		print 'Failed!'
	else:
		print 'OK.'

def lex_tests():
	lex_test('aexp > Aer : /[0-9]/ > ( Numeral n=$1)', ['aexp', '>', 'Aer', ':', '/[0-9]/', '>', '(','Numeral','n=','$',1,')'])
	lex_test('\'(\'',["'('"])
	lex_test("aexp | '(' aexp ')' > ( Mult a1=$1 a2=$3)" , ['aexp', '|', "'('", 'aexp', "')'",'>', '(', 'Mult', 'a1=','$',1, 'a2=','$',3,')'])
	lex_test("'true' > (LitTrue)",["'true'",'>','(','LitTrue',')'])
	lex_test("bexp '&' bexp > (Conjunt b1=$1 b2=$3)",['bexp', "'&'", 'bexp', '>', '(', 'Conjunt', 'b1=','$',1,'b2=','$',3,')'])	
	lex_test("'!' bexp > (Neg b=$2)",["'!'", 'bexp', '>', '(','Neg', 'b=','$',2,')'])
	lex_test("aexp '-' aexp > (Subtraction a1=$1 a2=$3)",['aexp',"'-'", 'aexp','>','(','Subtraction', 'a1=','$',1,'a2=','$',3,')'])
	lex_test("%nonassoc '=' '<=';",['%nonassoc',"'='","'<='",';'])
	lex_test("stmt > Statement: /[a-zA-Z]\w*/ ':=' aexp > (Assignment x=$1 a=$2)| 'skip' > (Skip)| 'if' bexp 'then' stmt 'else' stmt > (IfThenElse b=$2 s1=$4 s2=$6)| 'while' bexp 'do' stmt > (WhileDo b=$2 s=$4)| '{' seq '}' > $2;",["stmt", '>','Statement',':','/[a-zA-Z]\w*/', "':='",'aexp','>','(','Assignment', 'x=','$',1,'a=','$',2,')','|', "'skip'", '>','(','Skip',')','|',"'if'",'bexp',"'then'",'stmt',"'else'",'stmt','>','(','IfThenElse','b=','$',2,'s1=','$',4,'s2=','$',6,')','|',"'while'",'bexp',"'do'","stmt",'>','(','WhileDo','b=','$',2,'s=','$',4,')','|',"'{'",'seq',"'}'",'>',"$",2,";"])
	lex_test("%left '&'; // Lo menos precedente.\n",['%left',"'&'",';'])
	# Agregar más tests a medida que se implementan más funcionalidades
	# Ejemplos: 
		
if __name__ == '__main__':
	lex_tests()